import { Controller, Get, Post, Body, Put, ParseUUIDPipe, Param, ParseIntPipe, Delete, Query } from '@nestjs/common'
import { UserService } from './user.service'
import { CreateUserDto } from './dto/create-user.dto'
import { Public } from '@/decorator/public.decorator'

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get(':uid')
  findOne(@Param('uid', ParseUUIDPipe) uid: string) {
    return this.userService.findOne(uid)
  }

  @Get()
  findUser(@Query() query: { page?: number; limit?: number }) {
    const { page = 1, limit = 10 } = query
    return this.userService.findUser(page, limit)
  }

  @Public()
  @Post('register')
  createUser(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto)
  }

  @Public()
  @Post('login')
  login(@Body() createUserDto: CreateUserDto) {
    return this.userService.login(createUserDto)
  }

  @Post('search')
  searchUser(@Body('username') username: string = '', @Body('role_name') role_name: string = '') {
    return this.userService.searchUser(username, role_name)
  }

  @Put(':uid')
  changePassword(@Param('uid', ParseUUIDPipe) uid: string, @Body() body: { password: string }) {
    return this.userService.changePassword(uid, body.password)
  }

  @Put(':uid/state/:state')
  changeState(@Param('uid', ParseUUIDPipe) uid: string, @Param('state', ParseIntPipe) state: number) {
    return this.userService.changeState(uid, state)
  }

  @Put(':uid/role/:rid')
  changeRole(@Param('uid', ParseUUIDPipe) uid: string, @Param('rid', ParseIntPipe) rid: number) {
    return this.userService.changeRole(uid, rid)
  }

  @Delete(':uid')
  deleteOne(@Param('uid', ParseUUIDPipe) uid: string) {
    return this.userService.deleteUser(uid)
  }
}
